﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
using MetaInfoDAL;
using Microsoft.Office.Interop.Word;
using System.IO;
using SemantikRefAddIn.LocalJavaService;
using SemantikRefAddIn.SemanticRefService;
using System.Xml.Linq;

namespace SemantikRefAddIn
{
    public partial class SemantikRefTaskPane : UserControl
    {
        private MataInfoDAL metaInfo = new MataInfoDAL();
        private Object paramBiblioSourceTag;
        private string bibilographyXML;

        private List<MetaInfoListItem> itemList = new List<MetaInfoListItem>();



        public SemantikRefTaskPane()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            txtSearchInput.Clear();
            button1.Enabled = false;
        }

        // run the search 
        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (txtSearchInput.Text == "")
            {
                MessageBox.Show("Please input the search text");
            }
            else
            {
                dataGridView1.DataSource = null;
                dataGridView1.ClearSelection();
                rtxbContent.Clear();
                ////////////////////////////////////////////////////
                // call the Webservice and get the filePath
                string inputString = txtSearchInput.Text;
                SemanticRefService.SemanticRefClient webserviceClient = new SemanticRefClient();
                string filePath = webserviceClient.search(inputString);
                                  // "http://localhost:8080/result_48.xml";
                xmlToBibItem.ReadXML(filePath);

                int i = xmlToBibItem.getResultNumber();

                // int resultNumber = xtbi.getResultNumber();
                label4.Text = i.ToString();
                xmlToBibItem.GetTitleTagList(ref itemList);

                dataGridView1.DataSource = itemList;
            }
        }

        
        // Select a Document Source from dataGridView1
        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            // do nothing
        }

        // insert a Cite into the current text content.
        private void btnInsert_Click(object sender, EventArgs e)
        {
            object paramMissing = Type.Missing;
            object paramWdUnits = Word.WdUnits.wdStory;
            object paramWdMovementType = Word.WdMovementType.wdMove;
            object paramWdBreakType = Word.WdBreakType.wdPageBreak;
            object paramWdFieldTypeCitation = Word.WdFieldType.wdFieldCitation;
            object paramWdFieldTypeBiblio = Word.WdFieldType.wdFieldBibliography;
            String a = (String)paramBiblioSourceTag;

            Word.Application wordApplication = Globals.ThisAddIn.Application;

            Word.Document wordDocument = Globals.ThisAddIn.Application.ActiveDocument;
            // string bibXMLString = metaInfo.XgetSourceURL(title);
            // paramBiblioSourceTag = metaInfo.GetTagWithTitle(title);
            wordDocument.Bibliography.Sources.Add(bibilographyXML);

            // Insert a citation after the text just inserted to the bibliography
            // source added previously.
            Globals.ThisAddIn.Application.Selection.Fields.Add(wordApplication.Selection.Range,
                ref paramWdFieldTypeCitation, ref paramBiblioSourceTag, ref paramMissing);
        }

        private void txtSearchInput_TextChanged(object sender, EventArgs e)
        {
            if ((button1.Enabled == false) && (txtSearchInput.Text.Length != 0))
            {
                button1.Enabled = true;
            }

        }

        private void viewTextContent(string sourceURL)
        {
            FileStream fs = new FileStream(sourceURL, FileMode.Open, FileAccess.Read);
            StreamReader MyStreamReader = new StreamReader(fs);
            // load text eachline to eof, view in rtxContent
            rtxbContent.Text = "";
            string strLine = MyStreamReader.ReadLine();
            while (strLine != null)
            {
                rtxbContent.Text += strLine + "\n";
                strLine = MyStreamReader.ReadLine();
            }
            //close streamReader object;
            MyStreamReader.Close(); 
        }



        private static XMLToBibItem xmlToBibItem = new XMLToBibItem();

        private void dataGridView1_Click(object sender, EventArgs e)
        {
            string docTag = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            paramBiblioSourceTag = docTag;
            string sourceURL = xmlToBibItem.GetSourceURL(docTag);
            bibilographyXML = xmlToBibItem.GetBibXML(docTag);
            // rtxbContent.Text = sourceURL;
            viewTextContent(sourceURL);
            //////////////////////////////////////////////////////////////
            // view the text fragment in the box
            // rtxbContent.Text = sourceURL;
            MessageBox.Show(docTag);
        }
    }
}
